﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace CodeStash.Addin.MEF
{
    public class AddinViewModelLocator
    {

        #region ViewModelName

        public static readonly DependencyProperty ViewModelNameProperty =
            DependencyProperty.RegisterAttached("ViewModelName", typeof(string), typeof(AddinViewModelLocator),
                new FrameworkPropertyMetadata((string)"",
                    new PropertyChangedCallback(OnViewModelNameChanged)));


        public static string GetViewModelName(DependencyObject d)
        {
            return (string)d.GetValue(ViewModelNameProperty);
        }

        public static void SetViewModelName(DependencyObject d, string value)
        {
            d.SetValue(ViewModelNameProperty, value);
        }

        private static void OnViewModelNameChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var datacontext = MEFPartsResolver.Instance.GetExportForView((object)d, (string)e.NewValue);
            ((FrameworkElement)d).DataContext = datacontext;
        }

        #endregion


    }
}
